package com.baimifan.renovation;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.baimifan.renovation.adapter.TaskAdapter;
import com.baimifan.renovation.manager.SearchManager;
import com.baimifan.renovation.tools.statusBar;
import com.gyf.barlibrary.ImmersionBar;

import org.xutils.common.util.DensityUtil;

import java.lang.ref.WeakReference;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener{
    private Float w, h, widthPixels, heightPixels;
    private Activity search;
    private LinearLayout flag;
    private RelativeLayout search_rl;
    private ImageView search_tag;
    private FrameLayout search_title;
    private EditText search_et;
    private ImageView search_iv, search_clear;
    private RecyclerView recyclerView;
    private TaskAdapter taskAdapter;
    private SearchManager searchManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        search = SearchActivity.this;
        ImmersionBar.with(this).init();
        init();
        getDisplay();
        setLayoutParams();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        search_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchManager.getTask(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void init () {
        flag = (LinearLayout) findViewById(R.id.search_flag);
        search_rl = (RelativeLayout) findViewById(R.id.search_rl);
        search_tag = (ImageView) findViewById(R.id.search_tag);
        search_title = (FrameLayout) findViewById(R.id.search_title);
        search_et = (EditText) findViewById(R.id.search_et);
        search_iv = (ImageView) findViewById(R.id.search_iv);
        search_clear = (ImageView) findViewById(R.id.search_clear);
        recyclerView = (RecyclerView) findViewById(R.id.search_recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        searchManager = new SearchManager(new MyHandler(this));

        search_clear.setOnClickListener(this);
        search_tag.setOnClickListener(this);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setLayoutParams() {
        LinearLayout.LayoutParams params_flag = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                statusBar.getStatusBarHeight(search));
        flag.setLayoutParams(params_flag);

        LinearLayout.LayoutParams params_rl = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int)(h*44));
        search_rl.setLayoutParams(params_rl);

        RelativeLayout.LayoutParams params_tag = new RelativeLayout.LayoutParams((int)(w*8.5), (int)(h*16));
        params_tag.addRule(RelativeLayout.CENTER_VERTICAL);
        params_tag.setMargins((int)(w*15), 0, 0,0);
        search_tag.setLayoutParams(params_tag);

        LinearLayout.LayoutParams params_title = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_title.setMargins(0, 6, 0 ,0);
        search_title.setLayoutParams(params_title);

        FrameLayout.LayoutParams params_et = new FrameLayout.LayoutParams((int)(330*w),(int)(h*33));
        params_et.setMargins((int)(w*15), (int)(h*8), (int)(15), (int)(h*8));
        search_et.setPadding((int)(w*39), 0, (int)(w*50), 0);
        search_et.setLayoutParams(params_et);
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(Color.parseColor("#EFEFEF"));
        gd.setCornerRadius(DensityUtil.dip2px(16));
        search_et.setBackground(gd);

        FrameLayout.LayoutParams params_iv = new FrameLayout.LayoutParams((int)(w*18), (int)(h*18.1));
        params_iv.gravity = Gravity.LEFT | Gravity.CENTER;
        params_iv.setMargins((int)(w*30), 0, 0, 0);
        search_iv.setLayoutParams(params_iv);

        FrameLayout.LayoutParams params_clear = new FrameLayout.LayoutParams((int)(w*14), (int)(h*14));
        params_clear.gravity = Gravity.RIGHT | Gravity.CENTER;
        params_clear.setMargins(0, 0, (int)(w*30), 0);
        search_clear.setLayoutParams(params_clear);

        LinearLayout.LayoutParams params_recycler = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_recycler.setMargins(0, (int)(1.6), 0 , 0);
        recyclerView.setLayoutParams(params_recycler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_tag:
                finish();
                break;

            case R.id.search_clear:
                search_et.setText("");
                break;
        }
    }

    private class MyHandler extends Handler {
        WeakReference<SearchActivity> msearch;

        public MyHandler (SearchActivity searchActivity) {
            msearch = new WeakReference<SearchActivity>(searchActivity);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SearchActivity search = msearch.get();
            switch (msg.what) {
                case 9:
                    taskAdapter = new TaskAdapter(searchManager.gettasks(), w, h);
                    recyclerView.setAdapter(taskAdapter);
                    taskAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }
}
